(function($) {
    $.fn.jooTableClass = function(tableClass) {
        tableClass = (typeof(tableClass) == "undefined") ? "" : tableClass;
        this.each(function() {
            $(this).attr("tableclass", tableClass);
            $(this).addClass(tableClass);
        });
        return this;
    };
    
    $.fn.jooTableHeadClass = function(tableHeadClass) {
        tableHeadClass = (typeof(tableHeadClass) == "undefined") ? "" : tableHeadClass;
        this.each(function() {
            $(this).attr("tableheadclass", tableHeadClass);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if (rowIndex == 0 && colIndex == 0) {
                        $(this).addClass(tableHeadClass);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableRowHeadClass = function(rowHeadClass) {
        rowHeadClass = (typeof(rowHeadClass) == "undefined") ? "" : rowHeadClass;
        this.each(function() {
            $(this).attr("rowheadclass", rowHeadClass);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if (rowIndex != 0 && colIndex == 0) {
                        $(this).addClass(rowHeadClass);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableColHeadClass = function(colHeadClass) {
        colHeadClass = (typeof(colHeadClass) == "undefined") ? "" : colHeadClass;
        this.each(function() {
            $(this).attr("colheadclass", colHeadClass);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if (rowIndex == 0 && colIndex != 0) {
                        $(this).addClass(colHeadClass);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableCellClass = function(cellClass) {
        cellClass = (typeof(cellClass) == "undefined") ? "" : cellClass;
        this.each(function() {
            $(this).attr("cellclass", cellClass);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if (rowIndex != 0 && colIndex != 0) {
                        $(this).addClass(cellClass);
                    }
                });
            });
        });
        return this;
    };
    
    function jooTableEditor() {
        var input = document.createElement("textarea");
        var style = $(this).attr("style");
        style = (typeof(style) == "undefined") ? "" : style;
        $(input).attr("style", style);
        var className = $(this).attr("class");
        className = (typeof(className) == "undefined") ? "" : className;
        $(input).addClass(className);
        $(input).css("border", "0");
        $(input).css("border-style", "none");
        $(input).css("overflow", "hidden");
        $(input).height($(this).height() - 6);
        $(input).width($(this).width() - 6);
        $(input).val($(this).text());
        $(input).blur(function () {
            $(this).parent().text($(input).val());
            $(this).parent().empty();
        });
        $(input).keypress(function (e) {
            if(!e.ctrlKey && e.which == 13){   
                $(this).parent().text($(input).val());
                $(this).parent().empty();
            }
        });
        $(this).text("");
        $(this).empty();
        $(this).append(input);
        $(input).focus();
    };
    
    $.fn.jooTableHeadEditable = function(editable) {
        editable = (typeof(editable )== "undefined") ? true : editable;
        this.each(function() {
            $(this).attr("tableheadeditable", editable);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if ((rowIndex == 0 && colIndex == 0) && editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                        $(this).dblclick(jooTableEditor);
                    }
                    else if ((rowIndex == 0 && colIndex == 0) && !editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableRowHeadEditable = function(editable) {
        editable = (typeof(editable )== "undefined") ? true : editable;
        this.each(function() {
            $(this).attr("rowheadeditable", editable);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if ((rowIndex != 0 && colIndex == 0) && editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                        $(this).dblclick(jooTableEditor);
                    }
                    else if ((rowIndex != 0 && colIndex == 0) && !editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableColHeadEditable = function(editable) {
        editable = (typeof(editable )== "undefined") ? true : editable;
        this.each(function() {
            $(this).attr("colheadeditable", editable);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if ((rowIndex == 0 && colIndex != 0) && editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                        $(this).dblclick(jooTableEditor);
                    }
                    else if ((rowIndex == 0 && colIndex != 0) && !editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableCellEditable = function(editable) {
        editable = (typeof(editable )== "undefined") ? true : editable;
        this.each(function() {
            $(this).attr("celleditable", editable);
            $(this).children("tbody").children("tr").each(function() {
                var rowIndex = this.rowIndex;
                $(this).children("th,td").each(function() {
                    var colIndex = this.cellIndex;
                    if ((rowIndex != 0 && colIndex != 0) && editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                        $(this).dblclick(jooTableEditor);
                    }
                    else if ((rowIndex != 0 && colIndex != 0) && !editable) {
                        $(this).unbind("dblclick", jooTableEditor);
                    }
                });
            });
        });
        return this;
    };
    
    $.fn.jooTableInsertRow = function(rowIndex) {
        rowIndex = (typeof(rowIndex)=="undefined")?-1:rowIndex;
        this.each(function() {
            var aa = $(this).attr("cellClass");
            var newRow = this.insertRow(rowIndex);
            $(newRow).children("th,td").each(function() {
                var colIndex = this.cellIndex;
                if (colIndex == 0) {
                    $(this).addClass(cellClass);
                }
            });
        });
        return this;
    };
})(jQuery);